package com.bamboo.tool.view.component.actions;

import com.bamboo.tool.config.BambooToolComponent;
import com.bamboo.tool.config.model.RestfulCloudConfig;
import com.bamboo.tool.enums.RequestMethod;
import com.bamboo.tool.enums.SoaType;
import com.intellij.icons.AllIcons;
import com.intellij.ide.util.ElementsChooser;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.List;

/**
 * @author EDZ
 */
public class SoaTypeFilterAction extends ShowFilterAction {
    private Project project;
    private Runnable rebuildRunnable;

    public SoaTypeFilterAction(Runnable rebuildRunnable, Project project) {
        super("soa类型过滤器", "soa类型过滤器", AllIcons.General.Filter);
        this.project = project;
        this.rebuildRunnable = rebuildRunnable;
    }

    @Override
    protected boolean isEnabled() {
        return true;
    }

    @Override
    protected boolean isActive() {
        return true;
    }

    @Override
    protected ElementsChooser<?> createChooser() {
        RestfulCloudConfig restfulCloudConfig = BambooToolComponent.getInstance(project).getState();
        ElementsChooser<SoaType> res = new ElementsChooser<>(List.of(SoaType.values()), false) {
            @Override
            protected String getItemText(@NotNull SoaType value) {
                return value.getCode();
            }

            @Nullable
            @Override
            protected Icon getItemIcon(@NotNull SoaType value) {
                return value.getIcon();
            }
        };
        res.markElements(restfulCloudConfig.getSelectSoaTypes());
        ElementsChooser.ElementsMarkListener<SoaType> listener = (element, isMarked) -> {
            if (isMarked) {
                restfulCloudConfig.getSelectSoaTypes().add(element);
            } else {
                restfulCloudConfig.getSelectSoaTypes().remove(element);
            }
            rebuildRunnable.run();
        };
        res.addElementsMarkListener(listener);
        return res;
    }
}
